package com.cssw.fyzb.web.service;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.core.dao.appeal.AppealDao;
import com.cssw.fyzb.core.dao.appeal.AppealTypeDao;
import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.web.vo.AppealVo;
import com.cssw.fyzb.web.vo.FuncVo;
import com.cssw.fyzb.web.vo.UserVo;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AppealService {
    @Autowired
    AppealDao appealDao;
    @Autowired
    UserDao userDao;
    @Autowired
    AppealTypeDao appealTypeDao;

    public AjaxPage<AppealVo> list(int page, int limit, Map param) {
        Page<Appeal> list = appealDao.list(page, limit,param);
        List<AppealVo> funcVos = new ArrayList<>();
        if (null != list.getList() && list.getList().size() != 0) {
            AppealVo appealVo = null;
            for (Appeal func : list.getList()) {
                appealVo = new AppealVo();
                appealVo.setUserId(func.getUserId());
                appealVo.setAppealId(func.getAppealId());
                appealVo.setAppealAttach(func.getAppealAttach());
                appealVo.setAppealTime(func.getAppealTime());
                appealVo.setContact(func.getContact());
                appealVo.setStatus(func.getStatus());
                appealVo.setAppealTypeName(func.getTypeName());
                appealVo.setNickName(func.getNickName());
                appealVo.setAppealType(func.getAppealType());
                funcVos.add(appealVo);
            }
        }

        return AjaxPage.success(funcVos,list.getTotalRow(),list.getTotalPage());
    }


    public AjaxData<AppealVo> getDetails(Long appealId){
        Appeal appeal = appealDao.getAppeal(appealId);
        AppealVo appealVo = new AppealVo();
        appealVo.setAppealId(appeal.getAppealId());
        appealVo.setAppealAttach(appeal.getAppealAttach());
        appealVo.setAppealTime(appeal.getAppealTime());
        appealVo.setContact(appeal.getContact());
        appealVo.setStatus(appeal.getStatus());
        User user = userDao.getById(appeal.getUserId());
        appealVo.setNickName(user.getNickName());
        AppealType appealType= appealTypeDao.getById(appeal.getAppealType());
        appealVo.setAppealType(appeal.getAppealType());
        appealVo.setAppealTypeName(appealType.getAppealTypeName());
        return AjaxData.success(appealVo);
    }


    public boolean update(Long appealId,String feedback,Long perId){
        Appeal appeal = appealDao.getAppeal(appealId);
        appealDao.save(appealId,feedback,perId,"2");
            return true;
    }

}
